package com.ukao.steward.ui.menuLeft.myEarnings;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ukao.steward.R;
import com.ukao.steward.adapter.BalanceAdapter;
import com.ukao.steward.base.IAdapter;
import com.ukao.steward.base.IPage;
import com.ukao.steward.base.MvpFragment;
import com.ukao.steward.base.PageWrapper;
import com.ukao.steward.bean.IncomeDetailbBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.pesenter.valetRunners.BalanceListPesenter;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.view.valetRunners.BalanceListView;
import com.ukao.steward.widget.MyLRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.steward.consts.Constant.ARG_PARAM3;
import static com.ukao.steward.consts.Constant.ARG_PARAM4;
import static com.ukao.steward.consts.Constant.ARG_PARAM5;

/**
 * 收支明细
 */
public class BalanceListFragment extends MvpFragment<BalanceListPesenter> implements BalanceListView {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.lrecycler_view)
    MyLRecyclerView lrecyclerView;
    Unbinder unbinder;


    boolean loadFinish = false;
    private String mParam1;
    private String mParam2;
    private BalanceAdapter mBalanceAdapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private PageWrapper mPageWrapper;
    private View view;
    List<IncomeDetailbBean.ListBean> mDate = new ArrayList();
    private String tradeType;
    private String titleMsg;
    private String date;
    private String dateType;
    private int adapterType;
    private View mEmptyView;


    /**
     *
     * @param type   接口类型
     * @param dateType 1 是日 2是月 进入
     * @param date   接口时间
     * @param titleMsg  标题名称
     *      @param adapterType   适配器显示类型
     * @return
     */
    public static BalanceListFragment newInstance(String type, String dateType,String  date,String  titleMsg,int  adapterType) {
        BalanceListFragment fragment = new BalanceListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, type);
        args.putString(ARG_PARAM2, dateType);
        args.putString(ARG_PARAM3, date);
        args.putString(ARG_PARAM4, titleMsg);
        args.putInt(ARG_PARAM5, adapterType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            tradeType = getArguments().getString(ARG_PARAM1);
            dateType= getArguments().getString(ARG_PARAM2);
            date= getArguments().getString(ARG_PARAM3);
            titleMsg = getArguments().getString(ARG_PARAM4);
            adapterType= getArguments().getInt(ARG_PARAM5);
        }
    }

    @Override
    protected BalanceListPesenter createPresenter() {
        return new BalanceListPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_balance, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (loadFinish) return;
        lrecyclerView.forceToRefresh();
    }

    @Override
    protected void initView() {
        title.setText(CheckUtils.isEmpty(titleMsg) ? "余额明细" : titleMsg);
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
         mEmptyView= view.findViewById(R.id.empty_view);
        //lrecyclerView.setEmptyView(mEmptyView);
        initLinearRecyclerView(lrecyclerView);

        mBalanceAdapter = new BalanceAdapter(_mActivity, mDate,adapterType);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mBalanceAdapter);
        lrecyclerView.setAdapter(mLRecyclerViewAdapter);
        IPage page = new IPage() {
            @Override
            public void load(int geIndex, int pageSize) {
                mvpPresenter.incomeDetail(geIndex, pageSize, tradeType,date,dateType);

            }
        };
        mPageWrapper = new PageWrapper<>(mIAdapter, page);
        page.setPageSize(Constant.SEND_FACTORY_PAGE_SIZE);
    }





    @Override
    protected void initListener() {
        super.initListener();
        lrecyclerView.setOnRefreshListener(mOnRefreshListener);
        lrecyclerView.setOnLoadMoreListener(mOnLoadMoreListener);
        mLRecyclerViewAdapter.setOnItemClickListener(mOnItemClickListener);

    }


    private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            if (CheckUtils.isEmpty(dateType)){
                IncomeDetailbBean.ListBean mItem = mBalanceAdapter.getItem(position);
                start(IncomeDetailsFragment.newInstance(mItem, ""));
            }

        }
    };

    private OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {

            mPageWrapper.loadPage(true);
        }
    };
    private OnLoadMoreListener mOnLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            mPageWrapper.loadPage(false);
        }
    };


    IAdapter mIAdapter = new IAdapter() {
        @Override
        public void setDataSource(List data, boolean isNextLoad) {
            mEmptyView.setVisibility(CheckUtils.isEmpty(data)?View.VISIBLE:View.GONE);
            mBalanceAdapter.getDataList().clear();
            mBalanceAdapter.setDataList(data);
            mLRecyclerViewAdapter.notifyDataSetChanged();
            lrecyclerView.setLoadMoreEnabled(isNextLoad);


        }

        @Override
        public void loadMoreFinish(List data, boolean isNextLoad) {
            mBalanceAdapter.addAll(data);
            mLRecyclerViewAdapter.notifyDataSetChanged();
            if (!isNextLoad) lrecyclerView.setNoMore(!isNextLoad);
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void incomeSucceed(IncomeDetailbBean mIncomeDetailbBean) {
        loadFinish = true;
        mPageWrapper.addDataSource(mIncomeDetailbBean.getList(), mIncomeDetailbBean.getPages());
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        lrecyclerView.refreshFinish(Constant.SEND_FACTORY_PAGE_SIZE);

    }

    @OnClick({R.id.back_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;

        }
    }
}
